const express = require('express')

const router = express.Router()
const db = require('../../config/db.js')
// 引入jwt token工具
const JwtUtil = require('../../utils/jwt');

const dayjs = require('dayjs');


// 删除数据
router.delete('/delete', function (req, res) {
    // console.log(req.query);
    let jobnumber = req.query.jobnumber;
    let id = req.query.id;
    // console.log(jobnumber);
    // "delete from  数据表 where 字段 = '" + 值 + "'"
    let sql;
    if (id) {
        sql = 'delete from Serviceinfo where id = "' + id + '"';
    }
    // let sql = 'delete from User where jobnumber = "' + jobnumber + '"';
    db.query(sql, function (err) {
        if (err) {
            res.send({
                code: -1,
                msg: '删除失败',
                err,
            })
        } else {
            res.send({
                data: {
                    code: 200,
                    msg: '删除成功',
                    success: true,
                }
            })
        }
    })

})

// 增加服务名
router.post('/create', async function (req, res) {
    console.log(req.body);
    let servicename = req.body.servicename; // 服务名
    let fees = req.body.fees; // 费用
    let role = req.body.role; // 角色
    let id = dayjs().valueOf();
    // 查询用户表
    let user_check_sql = 'select * from service where servicename = "' + servicename + '"';
    db.query(user_check_sql, (err, rows) => {
        if (err) {
            res.send({
                code: -1,
                msg: '查询失败'
            })
        } else {
            // 解构赋值(数据库)
            if (rows && rows.length != 0) {
                let [user] = rows;
                if (user.servicename == servicename) {
                    res.send({
                        code: 201,
                        msg: '服务已存在'
                    })
                } else {
                    res.send({
                        code: 501,
                        msg: '后端异常'
                    })
                }
            }
            else {
                // 添加数据
                let user_insert_sql = 'insert into Service(servicename,id,fees,role) values(' + '"' + servicename + '"' + ',' + '"' + id + '"' + ',' + '"' + fees + '"' + ',' + '"' + role + '"' + ')';
                db.query(user_insert_sql, (err) => {
                    if (err) {
                        res.send({
                            code: -1,
                            msg: '添加失败',
                            err: err,
                        })
                    } else {
                        res.send({
                            data: {
                                code: 200,
                                msg: '添加成功',
                                success: true
                            }
                        });
                    }
                })
            }

        }
    })
})
// 增加上门服务信息
router.post('/serviceInfo', async function (req, res) {
    console.log(req.body);
    let servicename = req.body.servicename; // 服务名
    let name = req.body.name; // 科室名
    let classify = req.body.classify; // 科室类别
    let id = req.body.id;
    let jobnumber = req.body.jobnumber;
    let userName = req.body.userName;
    let img = req.body.img;
    let fees = req.body.fees;
    let role = req.body.role;
    let prove = req.body.prove;
    let content = req.body.content;
    // 查询用户表
    let user_check_sql = 'select * from serviceinfo where jobnumber = "' + jobnumber + '" and id = "' + id + '" and classify = "' + classify + '"';
    db.query(user_check_sql, (err, rows) => {
        if (err) {
            res.send({
                code: -1,
                msg: '查询失败'
            })
        } else {
            // 解构赋值(数据库)
            if (rows && rows.length != 0) {
                let [user] = rows;
                if (user.jobnumber == jobnumber && user.id == id && user.classify == classify) {
                    res.send({
                        code: 201,
                        msg: '上门服务信息已存在'
                    })
                } else {
                    res.send({
                        code: 501,
                        msg: '后端异常'
                    })
                }
            }
            else {
                // 添加数据
                let user_insert_sql = 'insert into Serviceinfo(name,id,classify,jobnumber,userName,img,servicename,fees,role,prove,content) values(' + '"' + name + '"' + ',' + '"' + id + '"' + ',' + '"' + classify + '"' + ',' + '"' + jobnumber + '"' + ',' + '"' + userName + '"' + ',' + '"' + img + '"' + ',' + '"' + servicename + '"' + ',' + '"' + fees + '"' + ',' + '"' + role + '"' + ',' + '"' + prove + '"' + ',' + '"' + content + '"' + ')';
                db.query(user_insert_sql, (err) => {
                    if (err) {
                        res.send({
                            code: -1,
                            msg: '添加失败',
                            err: err,
                        })
                    } else {
                        res.send({
                            data: {
                                code: 200,
                                msg: '添加成功',
                                success: true
                            }
                        });
                    }
                })
            }

        }
    })
})

//获取所有服务名
router.get('/serviceOptions', function (req, res) {
    // 前端接收参数
    let role = req.query.role;
    // 请求需要进行token校验 
    console.log("role", role);
    let user_check_sql = 'select * from service where role = "' + role + '"';
    console.log(user_check_sql);
    // 查询用户表
    //  user_check_sql = 'select * from department';
    db.query(user_check_sql, (err, rows) => {
        if (err) {
            res.send({
                code: -1,
                msg: '查询失败'
            })
        } else {
            // console.log(rows);
            // let [{ userName, gender, jobnumber, phone, avatar }] = rows;
            // console.log(typeof avatar);
            res.send({
                data: {
                    code: 200,
                    msg: '获取成功',
                    success: true,
                    data: {
                        result: rows
                    }
                }
            })
        }
    })
});

//获取所有上门服务信息
router.get('/serviceInfo', function (req, res) {
    // 前端接收参数
    let role = req.query.role;
    let searchKey = req.query.searchKey;
    console.log("searchKey", searchKey);

    let user_check_sql;
    if (searchKey && role) {
        user_check_sql = 'select * from serviceinfo where role = "' + role + '" and jobnumber = "' + searchKey + '" or id = "' + searchKey + '" or name = "' + searchKey + '" or userName = "' + searchKey + '" or classify = "' + searchKey + '" or servicename = "' + searchKey + '"';
    } else if (role) {
        user_check_sql = 'select * from serviceinfo where role = "' + role + '"';
    }
    // console.log(user_check_sql);
    // 查询用户表
    //  user_check_sql = 'select * from department';
    db.query(user_check_sql, (err, rows) => {
        if (err) {
            res.send({
                code: -1,
                msg: '查询失败'
            })
        } else {
            // console.log(rows);
            // let [{ userName, gender, jobnumber, phone, avatar }] = rows;
            // console.log(typeof avatar);
            let total = rows.length
            res.send({
                data: {
                    code: 200,
                    msg: '获取成功',
                    success: true,
                    data: {
                        result: rows,
                    }
                },
                success: true
            })
        }
    })
});

// 增加wx用户服务订单信息
router.post('/wxserviceInfo', async function (req, res) {
    console.log(req.body);
    let servicename = req.body.servicename; // 服务名
    let name = req.body.name; // 科室名
    let classify = req.body.classify; // 科室类别
    let id = dayjs().valueOf();
    let userId = req.body.userId;
    let jobnumber = req.body.jobnumber;
    let userName = req.body.userName;
    let realName = req.body.realName;
    let avgPath = req.body.avgPath;
    let fees = req.body.fees;
    let role = req.body.role;
    let phone = req.body.phone;
    let time = req.body.time;
    let address = req.body.address;
    let state = 2;
    // 查询用户表
    let user_check_sql = 'select * from booking where jobnumber = "' + jobnumber + '" and servicename = "' + servicename + '" and classify = "' + classify + '"';
    db.query(user_check_sql, (err, rows) => {
        if (err) {
            res.send({
                code: -1,
                msg: '查询失败'
            })
        } else {
            // 解构赋值(数据库)
            if (rows && rows.length != 0) {
                let [user] = rows;
                if (user.jobnumber == jobnumber && user.servicename == servicename && user.classify == classify) {
                    res.send({
                        code: 201,
                        msg: '订单服务信息已存在'
                    })
                } else {
                    res.send({
                        code: 501,
                        msg: '后端异常'
                    })
                }
            }
            else {
                // 添加数据
                let user_insert_sql = 'insert into Booking(name,id,classify,jobnumber,realName,avgPath,servicename,fees,role,phone,time,userId,address,state,userName) values(' + '"' + name + '"' + ',' + '"' + id + '"' + ',' + '"' + classify + '"' + ',' + '"' + jobnumber + '"' + ',' + '"' + realName + '"' + ',' + '"' + avgPath + '"' + ',' + '"' + servicename + '"' + ',' + '"' + fees + '"' + ',' + '"' + role + '"' + ',' + '"' + phone + '"' + ',' + '"' + time + '"' + ',' + '"' + userId + '"' + ',' + '"' + address + '"' + ',' + '"' + state + '"' + ',' + '"' + userName + '"' + ')';
                db.query(user_insert_sql, (err) => {
                    if (err) {
                        res.send({
                            code: -1,
                            msg: '添加失败',
                            err: err,
                        })
                    } else {
                        res.send({
                            data: {
                                code: 200,
                                msg: '添加成功'
                            },
                            success: true
                        });
                    }
                })
            }

        }
    })
})

// 增加wx用户挂号问诊信息
router.post('/wxregistered', async function (req, res) {
    console.log(req.body);
    let name = req.body.name; // 科室名
    let classify = req.body.classify; // 科室类别
    let id = dayjs().valueOf();
    let userId = req.body.userId;
    let jobnumber = req.body.jobnumber;
    let userName = req.body.userName;
    let realName = req.body.realName;
    let avgPath = req.body.avgPath;
    let phone = req.body.phone;
    let time = req.body.time;
    let address = req.body.address;
    let state = 2;
    // 查询用户表
    let user_check_sql = 'select * from registered where jobnumber = "' + jobnumber + '" and classify = "' + classify + '"';
    db.query(user_check_sql, (err, rows) => {
        if (err) {
            res.send({
                code: -1,
                msg: '查询失败'
            })
        } else {
            // 解构赋值(数据库)
            if (rows && rows.length != 0) {
                let [user] = rows;
                if (user.jobnumber == jobnumber && user.classify == classify) {
                    res.send({
                        code: 201,
                        msg: '挂号信息已存在'
                    })
                } else {
                    res.send({
                        code: 501,
                        msg: '后端异常'
                    })
                }
            }
            else {
                // 添加数据
                let user_insert_sql = 'insert into Registered(name,id,classify,jobnumber,realName,avgPath,phone,time,userId,address,state,userName) values(' + '"' + name + '"' + ',' + '"' + id + '"' + ',' + '"' + classify + '"' + ',' + '"' + jobnumber + '"' + ',' + '"' + realName + '"' + ',' + '"' + avgPath + '"' + ',' + '"' + phone + '"' + ',' + '"' + time + '"' + ',' + '"' + userId + '"' + ',' + '"' + address + '"' + ',' + '"' + state + '"' + ',' + '"' + userName + '"' + ')';
                db.query(user_insert_sql, (err) => {
                    if (err) {
                        res.send({
                            code: -1,
                            msg: '添加失败',
                            err: err,
                        })
                    } else {
                        res.send({
                            data: {
                                code: 200,
                                msg: '添加成功'
                            },
                            success: true
                        });
                    }
                })
            }

        }
    })
})

// 获取wx用户挂号问诊信息
router.get('/wxregistered', async function (req, res) {
    console.log(req.query);
    let searchKey = req.query.searchKey; // 关键字
    let userId = req.query.userId;
    let jobnumber = req.query.jobnumber;
    let role = req.query.role;
    let id = req.query.id;
    // 查询用户表
    let user_check_sql;
    if (searchKey) {
        user_check_sql = 'select * from registered where jobnumber = "' + searchKey + '" or id = "' + searchKey + '" or name = "' + searchKey + '" or userName = "' + searchKey + '" or classify = "' + searchKey + '"';
    } else if (userId) {
        user_check_sql = 'select * from registered where userId = "' + userId + '"';
    } else if (id) {
        user_check_sql = 'select * from registered where id = "' + id + '"';
    } else if (jobnumber) {
        user_check_sql = 'select * from registered where jobnumber = "' + jobnumber + '"';
    } else {
        user_check_sql = 'select * from registered';
    }
    db.query(user_check_sql, (err, rows) => {
        if (err) {
            res.send({
                code: -1,
                msg: '查询失败'
            })
        } else {
            res.send({
                data: {
                    result: rows,
                    success: true,
                },
                success: true,
                code: 200,
                msg: '获取成功',
            })

        }
    })
})

//获取wx用户上门服务信息
router.get('/wxserviceInfo', function (req, res) {
    // 前端接收参数
    let role = req.query.role;
    let searchKey = req.query.searchKey;
    let userId = req.query.userId;
    let id = req.query.id;
    let jobnumber = req.query.jobnumber;
    console.log("req.query", req.query);

    let user_check_sql;
    if (searchKey && role) {
        user_check_sql = 'select * from booking where role = "' + role + '" and jobnumber = "' + searchKey + '" or id = "' + searchKey + '" or name = "' + searchKey + '" or userName = "' + searchKey + '" or classify = "' + searchKey + '" or servicename = "' + searchKey + '"';
    } else if (role && jobnumber) {
        user_check_sql = 'select * from booking where role = "' + role + '" and jobnumber = "' + jobnumber + '"';
    } else if (role) {
        user_check_sql = 'select * from booking where role = "' + role + '"';
    } else if (userId) {
        user_check_sql = 'select * from booking where userId = "' + userId + '"';
    } else if (id) {
        user_check_sql = 'select * from booking where id = "' + id + '"';
    }
    console.log(user_check_sql);
    // 查询用户表
    //  user_check_sql = 'select * from department';
    db.query(user_check_sql, (err, rows) => {
        if (err) {
            res.send({
                code: -1,
                msg: '查询失败'
            })
        } else {
            // console.log(rows);
            // let [{ userName, gender, jobnumber, phone, avatar }] = rows;
            // console.log(typeof avatar);
            res.send({
                data: {
                    result: rows,
                    success: true,
                },
                success: true,
                code: 200,
                msg: '获取成功',
            })
        }
    })
});

// wx用户修改上门服务信息
router.put('/wxserviceInfo', async function (req, res) {
    console.log(req.body);
    let id = req.body.id; // ID
    let state = req.body.state || 0;
    let isfinish = req.body.isfinish;
    // let state = 0;
    // 查询用户表
    let user_check_sql = 'select * from booking where id = "' + id + '"';
    db.query(user_check_sql, (err, rows) => {
        if (err) {
            res.send({
                code: -1,
                msg: '查询失败'
            })
        } else {
            // 解构赋值(数据库)
            if (rows && rows.length != 0) {
                let [user] = rows;
                if (user.state == state) {
                    res.send({
                        data: {},
                        code: -1,
                        msg: "订单已取消，请无重复操作",
                        success: true,
                    });
                }
                else if (user.id == id) {
                    let user_update_sql;
                    if (isfinish) {
                        user_update_sql = 'update booking set isfinish = "' + isfinish + '" where id="' + id + '"';
                    } else if (state) {
                        user_update_sql = 'update booking set state = "' + state + '" where id="' + id + '"';
                    }
                    db.query(user_update_sql, function (err) {
                        if (err) {
                            res.send({
                                data: {
                                    code: -1,
                                    msg: "修改失败",
                                    success: false,
                                    err,
                                }
                            });
                        } else {
                            res.send({
                                data: {
                                    code: 200,
                                    msg: "修改成功",
                                    success: true,
                                },
                                code: 200,
                                msg: "修改成功",
                                success: true,
                            });
                        }
                    });
                } else {
                    res.send({
                        code: 501,
                        msg: '后端异常'
                    })
                }
            }
            else {
                res.send({
                    code: 404,
                    msg: '后端异常'
                })
            }

        }
    })
})

// 修改挂号信息状态
router.put('/wxregistered', async function (req, res) {
    console.log(req.body);
    let id = req.body.id; // ID
    let state = req.body.state;
    let reply = req.body.reply;
    // let state = 0;
    // 查询用户表
    let user_check_sql = 'select * from registered where id = "' + id + '"';
    db.query(user_check_sql, (err, rows) => {
        if (err) {
            res.send({
                code: -1,
                msg: '查询失败'
            })
        } else {
            // 解构赋值(数据库)
            if (rows && rows.length != 0) {
                let [user] = rows;
                if (user.state == state) {
                    res.send({
                        data: {},
                        code: -1,
                        msg: "订单已取消，请无重复操作",
                        success: true,
                    });
                }
                else if (user.id == id) {
                    let user_update_sql = "update registered set reply = '" + reply + "',state = '" + state + "' where  id = '" + id + "'";
                    db.query(user_update_sql, function (err) {
                        if (err) {
                            res.send({
                                data: {
                                    code: -1,
                                    msg: "修改失败",
                                    success: false,
                                    err,
                                }
                            });
                        } else {
                            res.send({
                                data: {
                                    success: true,
                                    code: 200,
                                    msg: "修改成功",
                                },
                                code: 200,
                                msg: "修改成功",
                                success: true,
                            });
                        }
                    });
                } else {
                    res.send({
                        code: 501,
                        msg: '后端异常'
                    })
                }
            }
            else {
                res.send({
                    code: 404,
                    msg: '后端异常'
                })
            }

        }
    })
})

module.exports = router;